package com.multysite.controller.me;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.NamespaceManager;
import com.multysite.entity.admin.Application;
import com.multysite.entity.admin.ApplicationConfig;
import com.multysite.entity.admin.ApplicationMapping;
import com.multysite.entity.admin.ApplicationTemplate;
import com.multysite.model.admin.ApplicationConfigModel;
import com.multysite.model.admin.ApplicationMappingModel;
import com.multysite.model.admin.ApplicationModel;
import com.multysite.model.admin.ApplicationTemplateModel;
import com.multysite.util.Setting;

public final class UrlFilter implements Filter {

	private static final Logger log = Logger.getLogger(UrlFilter.class
			.getName());

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String siteName = ((HttpServletRequest) request).getServerName();
		log.warning("---------------->" + siteName);
		if (!siteName.equals(Setting.getDomain())) {
			if (siteName.contains(Setting.getDomain())) {
				String subDomain = siteName.replace("." + Setting.getDomain(),
						"");
				Application obj = ApplicationModel.getById(subDomain);
				if (obj != null) {
					try {
						ApplicationConfig config = ApplicationConfigModel
								.getById(obj.getId(), obj.getId());
						ApplicationTemplate template = ApplicationTemplateModel
								.getById(obj.getId(), config.getTemplateId());
						NamespaceManager.set(obj.getId());
						if (config != null && template != null) {
							request.setAttribute("template", template);
							request.setAttribute("config", config);
						} else {
							response.getWriter().print(
									"Please try again later !");
							log.warning("Config or template are null !");
						}
					} catch (Exception e) {
						response.getWriter().print("Please try again later !");
						log.warning(e.toString());
					}
				} else {
					NamespaceManager.set(Setting.getGeneralNamespace());
				}
			} else {
				siteName = siteName.replace("http://", "");
				siteName = siteName.replace("www.", "");
				log.warning("---------------->------>" + siteName);
				ApplicationMapping mapping = ApplicationMappingModel
						.getById(siteName);
				if (mapping != null) {
					try {
						Application obj = ApplicationModel.getById(mapping
								.getApplicationId());
						if (obj != null) {
							ApplicationConfig config = ApplicationConfigModel
									.getById(obj.getId(), obj.getId());
							ApplicationTemplate template = ApplicationTemplateModel
									.getById(obj.getId(),
											config.getTemplateId());
							NamespaceManager.set(mapping.getApplicationId());
							if (config != null && template != null) {
								request.setAttribute("template", template);
								request.setAttribute("config", config);
							} else {
								response.getWriter().print(
										"Please try again later !");
								log.warning("Config or template are null !");
							}
						} else {
							response.getWriter().print("Invalid url !");
						}
					} catch (Exception e) {
						response.getWriter().print("Please try again later !");
						log.warning(e.toString());
					}
				} else {
					response.getWriter().print("Invalid url !");
				}
			}

		} else {
			NamespaceManager.set(Setting.getGeneralNamespace());
		}
		chain.doFilter(request, response);
		return;
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	}
}